/*
 * Copyright 2019-2021 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cool.houge.service.message.impl;

import cool.houge.grpc.MessageGrpc.MessageStub;
import cool.houge.grpc.MessagePb.SendMessageRequest;
import cool.houge.grpc.MessagePb.SendMessageRequest.ContentType;
import cool.houge.grpc.MessagePb.SendMessageResponse;
import cool.houge.grpc.SinkOneStreamObserver;
import cool.houge.service.message.SendMessageInput;
import cool.houge.service.message.SendMessageOutput;
import cool.houge.service.message.SendMessageService;
import javax.inject.Inject;
import reactor.core.publisher.Mono;

/**
 * 发送消息服务实现.
 *
 * @author KK (kzou227@qq.com)
 */
public class SendMessageServiceImpl implements SendMessageService {

  private final MessageStub messageStub;

  /**
   * 使用消息 gRPC 存根构造对象.
   *
   * @param messageStub 消息 gRPC 存根
   */
  public @Inject SendMessageServiceImpl(MessageStub messageStub) {
    this.messageStub = messageStub;
  }

  @Override
  public Mono<SendMessageOutput> sendToUser(SendMessageInput input) {
    return Mono.defer(
            () -> {
              var sink = new SinkOneStreamObserver<SendMessageResponse>();
              messageStub.sendToUser(mapInput(input), sink);
              return sink.asMono();
            })
        .map(this::mapResponse);
  }

  @Override
  public Mono<SendMessageOutput> sendToGroup(SendMessageInput input) {
    return Mono.defer(
            () -> {
              var sink = new SinkOneStreamObserver<SendMessageResponse>();
              messageStub.sendToGroup(mapInput(input), sink);
              return sink.asMono();
            })
        .map(this::mapResponse);
  }

  private SendMessageRequest mapInput(SendMessageInput input) {
    var contentType = ContentType.forNumber(input.getContentType());
    var builder =
        SendMessageRequest.newBuilder()
            .setFrom(input.getFrom())
            .setTo(input.getTo())
            .setContent(input.getContent())
            .setContentType(contentType);
    if (input.getExtraArgs() != null) {
      builder.setExtraArgs(input.getExtraArgs());
    }
    return builder.build();
  }

  private SendMessageOutput mapResponse(SendMessageResponse response) {
    return SendMessageOutput.builder().messageId(response.getMessageId()).build();
  }
}
